# 保存订单信息
from libs.db import DB

APP_PAY_MODE_MAP = {
    1: '余额支付',
    2: '微信支付',
    3: '支付宝支付',
    4: '现金支付',
    5: '仓储券支付',
    7: '线下扫码',
    99: '组合支付',
    10: '快餐店会员账户余额',
}


def save_app_order(cursor, app_order_data: dict):
    order_data = {
        'state': 7,
        'pay_mode': 0,
        'is_self_get': 1,
        'send_shop_id': -1,
        'address_id': 0,
        'sell_name': '',
        'sell_phone': '',
        'sell_province': '',
        'sell_city': '',
        'sell_area': '',
        'sell_address': '',
        'shipping_fee': 0,
        'remark': '',
        'order_type': 1,
        'deal_type':1,
        'meal_number':1,
        'is_quick':1,
    }
    order_data.update(app_order_data)
    db = DB('table_main_orders', conn=cursor)
    order_id, err = db.insert(order_data)
    if err is not None and db.is_duplicate:
        err = '订单数据已存在'
    return order_id, err


# 保存订单商品明细
def save_app_order_detail(cursor, app_order_detail_data: dict):
    order_detail_data = {
        'state': 7,
        'power_number': 0,
        'pay_mode': 0,
        'is_self_get': 1,
        'send_shop_id': -1,
        'address_id': 0,
        'sell_name': '',
        'sell_phone': '',
        'sell_province': '',
        'sell_city': '',
        'sell_area': '',
        'sell_address': '',
        'need_send_count': 0,
        'order_type': 1,
        'drink_type':1,
        'drink_flavor':'',
        'remark':''
    }
    order_detail_data.update(app_order_detail_data)
    db = DB('table_orders', conn=cursor)
    order_detail_id, err = db.insert(order_detail_data)
    return order_detail_id, err


def update_order_status(cursor, cs_order_id, status, app_order_data=None, filters=None):
    err = None
    app_order_new_data = {'state': status}
    order_data = {'state': status}
    if app_order_data is not None:
        pay_time = app_order_data.get('pay_time', None)
        if pay_time is not None:
            app_order_new_data.update({'pay_time': pay_time})
            order_data.update({'pay_time': pay_time})
    dh = DB('table_main_orders', conn=cursor)
    where_dict = {'cs_order_id': cs_order_id}
    if filters is not None:
        where_dict.update(filters)
    db_app_order = dh.one(['order_id'], where_dict)
    app_order_sn = db_app_order.get('order_id', '')
    if app_order_sn == '':
        err = 'App订单不存在'
        return err
    dh.update(app_order_new_data, where_dict)
    dh = DB('table_orders', conn=cursor)
    where_dict = {'order_id': app_order_sn}
    dh.update(app_order_new_data, where_dict)
    return err


def update_order_status_by_sn(cursor, cs_order_sn, status):
    err = None
    app_main_order_new_data = {'state': status}
    app_order_data = {'state': status}
    dh = DB('table_main_orders', conn=cursor)
    where_dict = {'order_id': cs_order_sn}
    db_app_order = dh.one(['id'], where_dict)
    app_main_order_id = db_app_order.get('id', 0)
    if app_main_order_id == 0:
        err = 'App主订单不存在'
        return err
    dh.update(app_main_order_new_data, where_dict)
    dh = DB('table_orders', conn=cursor)
    where_dict = {'order_id': cs_order_sn}
    dh.update(app_order_data, where_dict)
    return err


def get_app_main_order(cursor, cs_order_id):
    db = DB('table_main_orders', conn=cursor)
    fields = ['pay_mode', 'only_pay_no']
    return db.one(fields, 'cs_order_id=%s', [cs_order_id])
